package com.smart.manager.system.repository;

import cn.hutool.core.lang.Opt;
import com.smart.common.entities.core.Page;
import com.smart.manager.system.entities.body.RSystemScheduleRecord;
import com.smart.manager.system.entities.models.SystemScheduleRecord;
import com.smart.manager.system.entities.models.query.QSystemScheduleRecord;
import com.smart.utils.SqlUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author Administrator
 */
@Service @Log4j2
public class ScheduleRecordRepository {


    public Page<SystemScheduleRecord> pageList(RSystemScheduleRecord.SearchBody search) {
        QSystemScheduleRecord query = new QSystemScheduleRecord();
        Opt.ofNullable(search.getTaskId()).ifPresent(x -> query.task.id.eq(x));
        Opt.ofNullable(search.getRunMode()).ifPresent(x -> query.runMode.eq(x));
        Opt.ofNullable(search.getRunResult()).ifPresent(x -> query.runResult.eq(x));
        Opt.ofBlankAble(search.getRunDateRange()).ifPresent(x -> {
            LocalDateTime[] times = SqlUtil.formatDateTimeRange(x);
            query.runTime.between(times[0], times[1]);
        });
        query.setFirstRow(search.firstRow()).setMaxRows(search.maxRows()).order().id.desc();
        return new Page<SystemScheduleRecord>().load(query.findPagedList());
    }


}
